package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.WeekdaysAddDTO;
import org.jsola.hr.dto.WeekdaysUpdateDTO;
import org.jsola.hr.query.WeekdaysQuery;
import org.jsola.hr.service.IWeekdaysService;
import org.jsola.hr.vo.WeekdaysListVO;
import org.jsola.hr.vo.WeekdaysVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * 工作日历表
 *
 * @author zhr
 */
@RestController("hrWeekdaysController")
@RequestMapping(HrConstants.API_PATH + "/v1/weekdays")
public class WeekdaysController implements IWeekdaysControllerAPI {

    @Autowired
    private IWeekdaysService weekdaysService;

    @Override
    @PostMapping
    public Result<WeekdaysVO> save(@Valid @RequestBody WeekdaysAddDTO weekdaysAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工作日历表权限校验

        return Result.success(
                weekdaysService.save(weekdaysAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody WeekdaysUpdateDTO weekdaysUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工作日历表权限校验

        int count = weekdaysService.update(weekdaysUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{weekdaysId}")
    public Result<Integer> delete(@PathVariable String weekdaysId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工作日历表权限校验

        int count = weekdaysService.deleteByIds(tokenUser, weekdaysId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] weekdaysIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工作日历表权限校验

        int count = weekdaysService.deleteByIds(tokenUser, weekdaysIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{weekdaysId}")
    public Result<WeekdaysVO> findById(@PathVariable String weekdaysId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工作日历表权限校验

        return Result.success(
            weekdaysService.selectById(weekdaysId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<WeekdaysListVO>> listAll(@Valid WeekdaysQuery weekdaysQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工作日历表权限校验

        return Result.success(
            weekdaysService.select(weekdaysQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<WeekdaysListVO>> page(@Valid WeekdaysQuery weekdaysQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            weekdaysService.selectPage(weekdaysQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/workdayListIncludeEndDate")
    public Result<Integer> workdayListIncludeEndDate(Date startTime, Date endTime){

        // 查询
        return Result.success(
                weekdaysService.workdayListIncludeEndDate(startTime,endTime)
        );
    }
}

